package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.SpuInfoEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> wrapper = Wrappers.lambdaQuery();
        Long brandId = StringUtils.isEmpty((String) params.get("brandId"))? 0 :Long.parseLong((String) params.get("brandId")) ;
        Long catelogId = StringUtils.isEmpty((String) params.get("catelogId")) ? 0 : Long.parseLong((String) params.get("catelogId")) ;
        Long min = StringUtils.isEmpty((String) params.get("min")) ? 0 : Long.parseLong((String) params.get("min")) ;
        Long max = StringUtils.isEmpty((String) params.get("max")) ? 0 : Long.parseLong((String) params.get("max")) ;
        if (brandId != 0){
            wrapper.eq(SkuInfoEntity::getBrandId,brandId);
        }
        if (catelogId != 0){
            wrapper.eq(SkuInfoEntity::getCatalogId,catelogId);
        }
        if (!(min == 0 && max == 0)){
            wrapper.between(SkuInfoEntity::getPrice,min,max);
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.like(SkuInfoEntity::getSkuName,key);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

}